/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.user.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pingan.haofang.gov.sm.account.entity.domain.Account;
import com.pingan.haofang.gov.sm.account.entity.domain.AccountInfo;
import com.pingan.haofang.gov.sm.account.entity.domain.AccountRole;
import com.pingan.haofang.gov.sm.account.entity.dto.account.AccountDTO;
import com.pingan.haofang.gov.sm.account.entity.dto.account.AccountInfoDTO;
import com.pingan.haofang.gov.sm.account.entity.enums.YesNoEnum;
import com.pingan.haofang.gov.sm.account.repository.AccountRoleRepository;
import com.pingan.haofang.gov.sm.account.service.account.IAccountInfoService;
import com.pingan.haofang.gov.sm.account.service.account.IAccountService;
import com.pingan.haofang.searchcloud.user.dto.SearchUserDto;
import com.pingan.haofang.searchcloud.user.service.SearchUserService;

/**
 * 用户业务逻辑
 *
 * @author huhailiang937
 * @date 2017年5月2日下午5:51:54
 * @since 1.0.0
 */
@Service
public class SearchUserServiceImpl implements SearchUserService {
	@Autowired
	private IAccountInfoService accountInfoService;
    @Autowired
    private AccountRoleRepository accountRoleRepository;
	@Autowired
	private IAccountService accountService;

	private AccountDTO getAccount(Long accountId) {
		Account account = accountService.detail(accountId);
		if (Objects.isNull(account)) {
			return null;
		}
		AccountDTO accountDTO = new AccountDTO();
		BeanUtils.copyProperties(account, accountDTO);

		AccountInfo accountInfo = accountInfoService.getAccountInfo(accountId);
		if (accountInfo != null) {
			AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
			BeanUtils.copyProperties(accountInfo, accountInfoDTO);
			accountDTO.setAccountInfoDTO(accountInfoDTO);
		}
        AccountRole accountRole = accountRoleRepository.findOneByRoleIdAndAccountId(0L, accountId);
        if (accountRole != null) {
            accountDTO.setIsRoot(YesNoEnum.YES.getValue());
        } else {
            accountDTO.setIsRoot(YesNoEnum.NO.getValue());
        }
		return accountDTO;
	}

	private SearchUserDto transferDto(AccountDTO dto) {
		SearchUserDto result = new SearchUserDto();
		result.setId(dto.getId());
		result.setUserName(dto.getUsername());
		result.setTelephone(dto.getMobile());
		if(null !=  dto.getAccountInfoDTO()) {
			result.setEmail(dto.getAccountInfoDTO().getEmail());
		}
		result.setStatus(dto.getStatus());
		result.setRole(dto.getIsRoot());
		return result;
	}
	
	@Override
	public SearchUserDto getUserById(Long id) {
		AccountDTO dto = this.getAccount(id);
		return transferDto(dto);
	}


	@Override
	public List<SearchUserDto> getUserByIds(List<Long> ids) {
		List<SearchUserDto> dtos = new ArrayList<>();
		for (Long id : ids) {
			AccountDTO dto = this.getAccount(id);
			if(null != dto) {
				dtos.add(transferDto(dto));
			}
		}
		return dtos;
	}
	
	@Override
	public Map<Long, SearchUserDto> getUserMapByIds(Collection<Long> userIds) {
		List<SearchUserDto> dtos = new ArrayList<>();
		for (Long id : userIds) {
			AccountDTO dto = this.getAccount(id);
			if(null != dto) {
				dtos.add(transferDto(dto));
			}
		}
		return dtos.stream().collect(Collectors.toMap(SearchUserDto::getId, (s) -> s));
	}

}
